package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.java.dao.ActivitySlotDao;
import com.java.dao.ActivitySportDao;
import com.java.domain.ActivitySport;
import com.java.domain.event.ActivitySportEvent;
import com.java.service.CourseService;
import com.java.service.RoomService;
import com.java.service.SiteService;
import com.java.service.SlotService;
import com.java.service.SportService;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class SportServiceImpl implements SportService {

	@Resource ActivitySportDao sportDao;
	@Resource RoomService roomService;
	@Resource SiteService siteService;
	@Resource SlotService slotService;
	@Resource CourseService courseService;
	@Resource ActivitySlotDao slotDao;

	@Override
	public TableRespone getListByLast() {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sportPublish", 1);
		params.put("sportDel", 0);
		List<ActivitySportEvent> list = sportDao.selectAll(params, 0, 4);
		plugSport(list);
		long total = sportDao.selectAllCount(params);
		return new TableRespone(list, total);
	}

	@Override
	public TableRespone getListByType(Integer type, String title, Integer publish, int page, int size) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sportDel", 0);
		if(!MyTools.isEmpty(type)) {
			params.put("sportType", type);
		}
		if(!MyTools.isEmpty(publish)) {
			params.put("sportPublish", publish);
		}
		if(!MyTools.isEmpty(title)) {
			params.put("sportTitle", title);
		}
		List<ActivitySportEvent> list = sportDao.selectAll(params, page, size);
		plugSport(list);
		long total = sportDao.selectAllCount(params);
		return new TableRespone(list, total);
	}
	
	private void plugSport(List<ActivitySportEvent> list) {
		if(null != list) {
			for (ActivitySportEvent sport : list) {
				if(sport.getSportType() == 0) {
					sport.setRoom(roomService.getOne(sport.getSportRoomId()));
					sport.setCourse(courseService.getOne(sport.getSportCourseId()));
				}else {
					sport.setSite(siteService.getOne(sport.getSportSiteId()));
				}
			}
		}
	}

	@Override
	public ActivitySportEvent getOne(String sportId) {
		ActivitySportEvent info = sportDao.selectByKey(sportId);
		if(info.getSportType() == 0) {
			info.setRoom(roomService.getOne(info.getSportRoomId()));
			info.setCourse(courseService.getOne(info.getSportCourseId()));
		}else {
			info.setSite(siteService.getOne(info.getSportSiteId()));
		}
		return info;
	}

	@Override
	public BaseRespone saveSport(ActivitySport sport) {
		int row = 0;
		ActivitySport info = sportDao.selectByKey(sport.getSportId());
		if(null == info) {
			info = new ActivitySport()
			.setSportId(MyTools.getUUID())
			.setSportType(sport.getSportType())
			.setSportRoomId(sport.getSportRoomId())
			.setSportCourseId(sport.getSportCourseId())
			.setSportSiteId(sport.getSportSiteId())
			.setSportAdminId(ServletUtil.getAdminId())
			.setSportTitle(sport.getSportTitle())
			.setSportImg(sport.getSportImg())
			.setSportDesc(sport.getSportDesc())
			.setSportPublish(0)
			.setSportTime(new Date())
			.setSportDel(0);
			row = sportDao.insertSelective(info);
			if(row > 0) {
				slotService.initSlot(info.getSportId());
			}
		}else {
			info.setSportTitle(sport.getSportTitle())
			.setSportRoomId(sport.getSportRoomId())
			.setSportCourseId(sport.getSportCourseId())
			.setSportSiteId(sport.getSportSiteId())
			.setSportImg(sport.getSportImg())
			.setSportDesc(sport.getSportDesc());
			row = sportDao.updateSelective(info);
		}
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone sportPublish(String sportId, Integer publish) {
		ActivitySport info = new ActivitySport();
		info.setSportId(sportId)
		.setSportPublish(publish);
		if(publish == 1) {
			Map<String, Object> slotParams = new HashMap<String, Object>();
			slotParams.put("slotSportId", sportId);
			slotParams.put("slotEnable", 1);
			long stotal = slotDao.selectAllCount(slotParams);
			if(stotal == 0) {
				return BaseRespone.error("Not setting slot");
			}
			info.setSportPublishTime(new Date());
		}
		int row = sportDao.updateSelective(info);
		return BaseRespone.result(row, "Success");
	}

	@Override
	public BaseRespone delSport(String sportId) {
		int row = sportDao.deleteUpdate(sportId, "sport_del");
		return BaseRespone.result(row, "Success");
	}

	@Override
	public int getCountOfDay(String dateStr) {
		return sportDao.selectDateCount(dateStr);
	}

}
